package br.com.lpoint.job.message;

import java.util.HashMap;
import java.util.Map;
import java.util.ServiceLoader;

import javax.ejb.ActivationConfigProperty;
import javax.ejb.EJB;
import javax.ejb.MessageDriven;
import javax.jms.MapMessage;
import javax.jms.Message;

import br.com.document.job.JobDocument;
import br.com.lpoint.job.execute.JobProcess;


@MessageDriven(
		mappedName="jms/QueueJob",
	activationConfig = {
		@ActivationConfigProperty(propertyName = "acknowledgeMode",  propertyValue = "Auto-acknowledge"),
		@ActivationConfigProperty(propertyName = "destinationType",  propertyValue = "javax.jms.Queue"),
		@ActivationConfigProperty(propertyName = "clientId",  propertyValue = "JobMessageDriveBean"),
		@ActivationConfigProperty(propertyName = "subscriptionName",  propertyValue = "JobMessageDriveBean"),
		/*@ActivationConfigProperty(propertyName = "messageSelector",  propertyValue = "MessageFormat = 'com.kaizen.gollingost.jpa.model.sas.CidadesAfetadas'")*/
	}, messageListenerInterface=javax.jms.MessageListener.class
		)
public class JobMessageDriveBeanImpl implements JobMessageDriveBean {
	
	@EJB(beanInterface=JobProcess.class)
	private JobProcess processador;

	@Override
	public void onMessage(Message message) {
		try {
			
			
			MapMessage msg = (MapMessage) message;
			String nomeDoJob = msg.getString("nome");
			System.out.println("Recebendo mensagem com o nome: "+ nomeDoJob);
			//TODO: MONTAR ESTRUTURA PARA RECUPERAR PARAMETROS DIRETO DO MONGODB.
			Map<String, Object> params = new HashMap<String, Object>();		
			Class<JobDocument> jobClazz = (Class<JobDocument>) Class.forName(nomeDoJob);
			JobDocument jobObject = jobClazz.newInstance();
			processador.processaJob(jobObject, params);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
